package BNS.surveys;

import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.lang.Thread;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import BNS.plumbing.*;

/** This is an example class that implements a simple graph coloring game.
The game is solved when all players have colored their node with a color different from
each of their neighbours. 
It assumes that the colors are integers in {1,2, ..., colorChoices}. 
The counterpart class in the client machine is an BNSPersona known as SurveyPersona.
*/
public class SurveyGame extends BNSGameController implements SurveyGameInterface {
	protected String[] queries;
	protected PrintStream output;	// where record of game will be stored
	protected static final long serialVersionUID = 01L;
	
	public SurveyGame() {
		super();
		gameValue= "unsolved";
		}
	
	protected boolean initQueries() { 
		return true;
		}
	
	protected void saveGame() {
		System.err.println("game obtained: "+ getTitle());
		Library.init();
		Library.putGame((javax.swing.JFrame)null,this);
		System.err.println("game saved");
		}
	
	/** a routine that gets called at run time, to finish setting up
		things that cannot be initialized in the constructor
		(viz. the constructor should not create any GUI elements.)
		@param host	the Session Host, which can be queried for information about 
					the clients, players, etc. that are assigned to this game.
		@param locn the recommended pixel location for the top left 
					corner of the monitor GUI. */	
	public void init(SessionHost host, Point locn, PrintStream logFile) {
		this.host= host;
		monitor= new Monitor(locn);
		output= logFile;
		output.println(getTitle());
		output.println(new Date());
		}
		
	public int getSize() { return 0; }
	public String getTitle() { return "SurveyGame"; }
	/** gives a long description of this game; used to inform supervisor about game choice. */
	public String getInfo() { 
		return getTitle() +" number of queries= "+ queries.length +"\n";
		}
	
	public int cycleTime() { return 1000; }	// 50 msec is FAST; 200 is moderate
	
	/** The method that implements player actions in this survey game. 
	It accepts answers as they are entered, stores them, and changes the question. */
	public boolean reply(int clientID, int qNumber, String answer) throws RemoteException {
		System.err.println(" client #"+ clientID +" answering "+qNumber);
//		System.err.println(answer);
		nEvents++;
		output.println(clientID+" "+ qNumber);
		output.println(answer);
		output.println("**************************************************");
		monitor.repaint();
		return true;
		}
		
	/** called (indirectly) by Client to get data relevant to a node. 
	Irrelevant to the SurveyGame, but required by BNSGameController.
	@param clientID identifying the player.
	@return the system time, for no reason. */
	public java.io.Serializable getUpdate(int clientID) {
		return System.currentTimeMillis();	// something dumb
		}
	
	/** method required by BNSGameController interface.
	@return the BNSPersona object that runs in the client to implement
	the GUI of this game to the player */
	public BNSPersona getPersona(int clientID) {
		host.findClient(clientID);
		System.err.println("creating new SurveyPersona() for "+ clientID);
		return new SurveyPersona(this, clientID);
		}
	
	public float getPayoff(int clientID) { return 0.0f; }
	
	public static void main(String[] args) {
		SurveyGame game= new SurveyGame();
		game.saveGame();
		System.exit(0);
		}
	
	} // EOC SurveyGame
